package com.tzz.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.tzz.dao.DoOrderDao;
import com.tzz.entity.DoOrder;
import com.tzz.entity.SkuQty;
import com.tzz.service.DoOrderService;
import com.tzz.utils.ModelDataUtil;
import com.tzz.utils.ProducerMQUtil;
import com.tzz.vo.SkuQtyVo;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class DoOrderServiceImpl implements DoOrderService {

    @Autowired
    DoOrderDao doOrderDao;

    @Transactional
    @Override
    public void produceDoOrder(DoOrder doOrder) {
        JSONObject dataModel = getDoRequestBody(doOrder);
        DefaultMQProducer producer = ProducerMQUtil.getProducer("presale-test-do");
        ProducerMQUtil.sendMsg(producer,"10.23.30.48:9876;10.23.30.208:9876","fdcServiceAsnCreate",dataModel);
        ProducerMQUtil.producerShutDown(producer);
        Date createTime = new Date(System.currentTimeMillis());
        doOrder.setCreateTime(createTime);
        doOrderDao.insertDoOrder(doOrder);
        ArrayList<SkuQtyVo> skuQtyVos = new ArrayList<>();
        for (int i = 0; i < doOrder.getSkuQtyList().size(); i++) {
            SkuQtyVo skuQtyVo = new SkuQtyVo.Builder().
                    setAsnId(doOrder.getAsnId()).
                    setSkuId(doOrder.getSkuQtyList().get(i).getSkuId()).
                    setQty(doOrder.getSkuQtyList().get(i).getQty()).
                    build();
            skuQtyVos.add(skuQtyVo);
        }
        doOrderDao.insertSkuQtyList(skuQtyVos);

    }

    @Override
    public List<DoOrder> getDoOrders(HashMap<String, Object> asnId) {
        return doOrderDao.getDoOrders(asnId);
    }

    @Override
    public List<SkuQty> findSkuQty(HashMap<String, Object> asnId) {
        return doOrderDao.getSkuQtyByAsnId(asnId);
    }


    /**
     * 构建do单请求数据
     * */
    public JSONObject getDoRequestBody(DoOrder doOrder){
        JSONObject dataModel = ModelDataUtil.getRequestDataModel("do_order");
//        String doOrderId = RandomUtil.getDoOrderId();
        dataModel.put("asnNo",doOrder.getAsnId());
        dataModel.put("warehouseId",doOrder.getWarehouseId());
        dataModel.put("fromWarehouseId",doOrder.getFromWarehouseId());
        dataModel.put("planCheckoutTime",doOrder.getPlanCheckoutTime());
        ArrayList<Object> goodDetailList = new ArrayList<>();
        dataModel.put("asnDetailList",goodDetailList);
        for (int i = 0; i < doOrder.getSkuQtyList().size(); i++) {
            Map<String, Object> goodDetail = getGoodDetail();
            goodDetail.put("skuId",doOrder.getSkuQtyList().get(i).getSkuId());
            goodDetail.put("qty",doOrder.getSkuQtyList().get(i).getQty());
            goodDetailList.add(goodDetail);
        }
        System.out.println(dataModel);
        return dataModel;
    }


    /**
     * 设置商品模板
     * */
    private Map<String,Object> getGoodDetail(){
        HashMap<String, Object> goodDetail = new HashMap<>();
        goodDetail.put("skuId",4);
        goodDetail.put("scmLotNo","230104");
        goodDetail.put("wmsLotId","testtzz");
        goodDetail.put("qty",1.0);
        goodDetail.put("qty",8.0);
        return goodDetail;
    }
}
